package com.zs.java21;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author madison
 * {@code @description}
 * {@code @date} 2023/9/24 13:36
 */
public class VirtualThreadTest {
    static List<Integer> list = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException {
        CustomThread customThread = new CustomThread();
        // 使用Thread.startVirtualThread()创建
//        Thread.startVirtualThread(customThread);
        // 创建不启动
//        Thread unStarted = Thread.ofVirtual().unstarted(customThread);
//        unStarted.start();
        // 创建直接启动
//        Thread.ofVirtual().start(customThread);

        // 使用Executors.newVirtualThreadPerTaskExecutor()创建
        try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
            executor.submit(customThread);
        }

//        TimeUnit.SECONDS.sleep(2);

        compareTheadTest();
    }

    public static void compareTheadTest() {
        // 开启线程 统计平台线程数
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
            ThreadInfo[] threadInfo = threadMXBean.dumpAllThreads(false, false);
            updateMaxThreadNum(threadInfo.length);
        }, 10, 10, TimeUnit.MILLISECONDS);

        long start = System.currentTimeMillis();
        // 虚拟线程
//        try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
            // 使用平台线程
        // Platform Thread  线程数200、500、1000、2000
      try (ExecutorService executor = Executors.newFixedThreadPool(500)) {
            for (int i = 0; i < 10000; i++) {
                executor.submit(() -> {
                    try {
                        // 线程睡眠 0.5 s，模拟业务处理
                        TimeUnit.MILLISECONDS.sleep(1000);
                    } catch (InterruptedException interruptedException) {

                    }
                });
            }
            executor.close();
            System.out.println("max: " + list.get(0) + " platform thread/os thread");
            System.out.printf("totalMillis: %dms\n", System.currentTimeMillis() - start);
            executor.shutdown();
        }
    }

    // 更新创建的平台最大线程数
    private static void updateMaxThreadNum(int num) {
        if (list.isEmpty()) {
            list.add(num);
        } else {
            Integer integer = list.get(0);
            if (num > integer) {
                list.add(0, num);
            }
        }
    }
}

class CustomThread implements Runnable {
    @Override
    public void run() {
        System.out.println("CustomThread run");
    }
}
